package com.shengde.jinwawaforleader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.homePage.FindPasswordActivity;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.model.myJob.instance_message.DemoHelper;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProLogin;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import static com.shengde.jinwawaforleader.HomeApplication.showToastShort;

/**
 * 登陆界面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

	private static final String TAG ="LoginActivity" ;
	private Button btn_login;
	private EditText et_login_phone;
	private EditText et_login_password;
	private TextView tv_forget_password; //忘记密码按钮

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.activity_login);
	}

	@Override
	public void initView() {
		btn_login = (Button) findViewById(R.id.btn_login);
		et_login_phone = (EditText) findViewById(R.id.et_login_phone);
		et_login_password = (EditText) findViewById(R.id.et_login_password);
		tv_forget_password = (TextView) findViewById(R.id.tv_forget_password);
		String phone = User.getUserPhone(LoginActivity.this);
		if (!TextUtils.isEmpty(phone)) {
			et_login_phone.setText(phone);
			et_login_password.requestFocus();
		}
	}

	@Override
	public void initData() {
		if (getIntent().getBooleanExtra(Constant.ACCOUNT_CONFLICT, false)) {
			DemoHelper.getInstance().logout(false,null);
			Toast.makeText(this, "用户在其他设备登陆", Toast.LENGTH_SHORT).show();
		}
		if (User.getMiMa(LoginActivity.this)!=null){
			getIntentCallBack().startActivityFinishCommon(MainActivity.class);
			return;
		}
	}


	@Override
	public void initListener() {
		btn_login.setOnClickListener(this);
		tv_forget_password.setOnClickListener(this);

	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()){
			case R.id.btn_login:
				login();
				break;
			case R.id.tv_forget_password:
				Intent intent = new Intent(this,FindPasswordActivity.class);
				startActivity(intent);
				break;
		}
	}

	/**
	 * 点击登陆 后调用的方法
	 */
	private void login() {
		final String phone = et_login_phone.getText().toString();
		final String pwd = et_login_password.getText().toString();
		if (phone.equals("")) {
			showToastShort(getString(R.string.phone));
			return;
		}
		if (pwd.equals("")) {
			showToastShort(getString(R.string.password));
			return;
		}
		String url = "";
		if(User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST+"/director/directorLogiin";
		}else {
			url = BaseProtocol.HOST_BASE+"/director/directorLogiin";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("phone",phone);
		params.addParameter("pwd",pwd);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProLogin pro_login = new Gson().fromJson(result, ProLogin.class);
				Log.i(TAG, "onSuccess: 1....."  + pro_login.getCode());
				if (pro_login!=null&&pro_login.getCode()==0){
					//幼儿园名字
					User.setSchoolName(LoginActivity.this,pro_login.getSchoolname());
					//用户id
					User.setUserLoginAccount(LoginActivity.this,pro_login.getUserid() + "");
					//学校id
					User.setSchoolId(LoginActivity.this,pro_login.getSchoolid()+"");
					//登陆密码
					User.setMiMa(LoginActivity.this,pwd);
					//登陆账号
					User.setUserPhone(LoginActivity.this,phone);
					//用户头像
					User.setUserPhoto(LoginActivity.this,pro_login.getLogo());
					//用户名字
					User.setUserNickName(LoginActivity.this,pro_login.getName());

					getIntentCallBack().startActivityFinishCommon(
							MainActivity.class);
				}else {
					showToastShort(getString(R.string.testing));
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});

	}
}
